import 'package:enjoy_plan/locale/app_localizations.dart';
import 'package:enjoy_plan/page/route/router.dart';
import 'package:enjoy_plan/page/util/ui_adapt.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_login/flutter_login.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

import 'home_page.dart';

/// @author: lijinxi.1220@bytedance.com
/// @description:
/// @date: 2024/11/14 16:50

class LoginPage extends StatelessWidget {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
      dynamic isLarge = constraints.maxWidth > UIAdapt.minWidth;
      return Stack(children: [
        LoginWidget(),
        TopBarWidget(
          isLarge: isLarge,
        ),
        Align(
          alignment: Alignment.bottomCenter,
          child: BottomBarWidget(isLarge: isLarge),
        ),
      ]);
    });
  }
}

class BottomBarWidget extends StatelessWidget {
  final bool isLarge;

  const BottomBarWidget({
    super.key,
    required this.isLarge,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.blueAccent,
        height: 66,
        child: Center(
            child: Text(
          AppLocalizations.of(context).phase,
          style: TextStyle(
              fontSize: UIAdapt.fontSize(large: isLarge, size: 24, ratio: 0.66),
              fontWeight: FontWeight.w500,
              fontStyle: FontStyle.italic,
              decoration: TextDecoration.none,
              color: Colors.white),
        )));
  }
}

class TopBarWidget extends StatelessWidget {
  final bool isLarge;

  const TopBarWidget({
    super.key,
    required this.isLarge,
  });

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Container(
            color: Colors.blueAccent,
            padding: EdgeInsets.only(top: 8, left: 16, right: 16, bottom: 8),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.max,
              children: [
                Icon(
                  FontAwesomeIcons.bookOpen,
                  color: Colors.white,
                ),
                SizedBox(
                  width: 16,
                ),
                Text(
                  AppLocalizations.of(context).title,
                  style: TextStyle(
                      fontSize: UIAdapt.fontSize(large: isLarge, size: 24),
                      fontWeight: FontWeight.w500,
                      decoration: TextDecoration.none,
                      color: Colors.white),
                ),
                Spacer(),
                MenuItemWidget(
                  text: AppLocalizations.of(context).about,
                  fontSize: UIAdapt.fontSize(large: isLarge, size: 20),
                ),
                MenuItemWidget(
                  text: AppLocalizations.of(context).contact,
                  fontSize: UIAdapt.fontSize(large: isLarge, size: 20),
                ),
                MenuItemWidget(
                  text: AppLocalizations.of(context).privacy,
                  fontSize: UIAdapt.fontSize(large: isLarge, size: 20),
                ),
              ],
            )));
  }
}

class LoginWidget extends StatefulWidget {
  const LoginWidget({
    super.key,
  });

  @override
  State<LoginWidget> createState() => _LoginWidgetState();
}

class _LoginWidgetState extends State<LoginWidget> {
  @override
  Widget build(BuildContext context) {
    return FlutterLogin(
      scrollable: false,
      validateUserImmediately: true,
      userType: LoginUserType.email,
      headerWidget: null,
      onSubmitAnimationCompleted: () {},
      onLogin: (LoginData data) {
        // Handle login logic
        if (mounted) {
          Navigator.pushReplacementNamed(context, RouteConstants.home.str,
              arguments: data);
        }
      },
      onSignup: (_) {
        // Handle signup logic
        return Future.value(null);
      },
      onRecoverPassword: (String password) {
        // Handle password recovery logic
        return Future.value(null);
      },
      loginProviders: [
        LoginProvider(
            label: AppLocalizations.of(context).withGoogle,
            icon: FontAwesomeIcons.googlePlusG,
            callback: () async {
              return null;
            }),
      ],
      messages: LoginMessages(
        signupButton: AppLocalizations.of(context).signup,
        passwordHint: AppLocalizations.of(context).password,
        loginButton: AppLocalizations.of(context).login,
        userHint: AppLocalizations.of(context).email,
        forgotPasswordButton: AppLocalizations.of(context).forgetPassword,
        providersTitleFirst: AppLocalizations.of(context).otherLogin,
      ),
      theme: LoginTheme(
          cardTheme: CardTheme(color: Colors.white),
          buttonTheme: LoginButtonTheme(backgroundColor: Colors.blueAccent),
          primaryColor: Colors.blueAccent,
          pageColorLight: Colors.white,
          pageColorDark: Colors.white),
    );
  }
}

class MenuItemWidget extends StatelessWidget {
  final double space;
  final double fontSize;
  final FontWeight fontWeight;
  final Color textColor;
  final VoidCallback? onPressed;
  final String text;

  const MenuItemWidget({
    required this.text,
    this.onPressed,
    this.space = 0,
    this.fontSize = 20,
    this.fontWeight = FontWeight.w500,
    this.textColor = Colors.white,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        TextButton(
          onPressed: onPressed,
          child: Text(
            text,
            style: TextStyle(
                fontSize: fontSize,
                fontWeight: FontWeight.w500,
                color: Colors.white),
          ),
        ),
        SizedBox(
          width: space,
        ),
      ],
    );
  }
}
